package org.jeecg.modules.ncmeapis.processTest.business.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import ncme.digital.hszy.common.utils.DateUtils;
import ncme.digital.hszy.ep.enums.dict.ProcessDict;
import ncme.digital.hszy.ep.interfaces.NodeHandler;
import ncme.digital.hszy.ep.pre.StartNode;
import org.jeecg.modules.ncmeapis.processTest.processDefinition.commonNodes.DirectorApproveNode;
import org.jeecg.modules.ncmeapis.processTest.business.entity.HolidayForm;
import org.jeecg.modules.ncmeapis.processTest.processDefinition.holiday.HolidayProcess;
import org.jeecg.modules.ncmeapis.processTest.processDefinition.holiday.nodes.ApplyHolidayNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Api(tags = "请假流程")
//@BusinessApi
//@RestController
@RequestMapping("/demo/holiday")
@Slf4j
public class HolidayController {

    @Autowired
    private HolidayProcess holidayProcess;

    @ApiOperation(value = "请假流程定义")
    @GetMapping(value = "/defineHolidayApply")
    public Object defineHolidayApply() throws Exception {
        return holidayProcess.getStringChains(new StartNode());
    }

    @ApiOperation(value = "1开启请假流程")
    @GetMapping(value = "/startHolidayApply")
    public Object startHolidayApply() throws Exception {
        String operator = "小明";
        Integer processId = holidayProcess.start(operator);
        //正常应该先查询个人任务拿到processId
        holidayProcess.push(processId, operator, ApplyHolidayNode.class, new NodeHandler<ApplyHolidayNode>() {
            @Override
            public boolean Handle(ApplyHolidayNode processNode) throws Exception {
                HolidayForm holidayForm = new HolidayForm();
                holidayForm.setBeginDate(new Date());
                holidayForm.setName(operator);
                holidayForm.setType("事假");
                holidayForm.setEndDate(DateUtils.changeDate(new Date(), 4));
                processNode.setHolidayForm(holidayForm);
                processNode.setComment("我想请四天假");
//                processNode.setNextAssignee("小红");
                return true;
            }
        });
        return "ok，processId: " + processId;
    }

    @ApiOperation(value = "1重填请假单")
    @GetMapping(value = "/reFillHolidayApply")
    public Object reFillHolidayApply(@RequestParam Integer processId) throws Exception {
        String operator = "小明";
        //正常应该先查询个人任务拿到processId
        holidayProcess.push(processId, operator, ApplyHolidayNode.class, new NodeHandler<ApplyHolidayNode>() {
            @Override
            public boolean Handle(ApplyHolidayNode processNode) throws Exception {
                HolidayForm holidayForm = new HolidayForm();
                holidayForm.setBeginDate(new Date());
                holidayForm.setName(operator);
                holidayForm.setType("事假");
                holidayForm.setEndDate(DateUtils.changeDate(new Date(), 2));
                processNode.setHolidayForm(holidayForm);
                processNode.setComment("好吧我只请两天");
//                processNode.setNextAssignee("小红");
                return true;
            }
        });
        return "ok，processId: " + processId;
    }

    @ApiOperation(value = "2-1院长退回")
    @GetMapping(value = "/deanDenyHolidayApply")
    public Object deanDenyHolidayApply(@RequestParam Integer processId) throws Exception {
        String operator = "院长";
        holidayProcess.rollbackTo(processId, operator, ApplyHolidayNode.class, new NodeHandler<ApplyHolidayNode>() {
            @Override
            public boolean Handle(ApplyHolidayNode processNode) {
                processNode.putRemark("msg", "4天太长了，项目做不完");
                return true;
            }
        });
        return "ok";
    }

    @ApiOperation(value = "2-2所长审批")
    @GetMapping(value = "/directorAgreeHolidayApply")
    public Object directorAgreeHolidayApply(@RequestParam Integer processId) throws Exception {
        holidayProcess.push(processId, "所长", DirectorApproveNode.class);
        return "ok";
    }

    @ApiOperation(value = "3-1请假存档")
    @GetMapping(value = "/recordHolidayApply")
    public Object recordHolidayApply(@RequestParam Integer processId) throws Exception {
        holidayProcess.push(processId, "档案管理员");
        return "ok";
    }

    @ApiOperation(value = "3-2办公室确认")
    @GetMapping(value = "/officerAssert")
    public Object officerAssert(@RequestParam Integer processId) throws Exception {
        holidayProcess.push(processId, "小静");
        return "ok";
    }

    @ApiOperation(value = "办公室查任务")
    @GetMapping(value = "/getOfficerTasks")
    public Object getOfficerTasks() throws Exception {
        return holidayProcess.getPersonProcesses("小静");
    }

    @ApiOperation(value = "查看流程状态")
    @GetMapping(value = "/getProcessState")
    public Object getProcessState(@RequestParam Integer processId) throws Exception {
        return holidayProcess.getProcessChain(processId);
    }

    @ApiOperation(value = "抄送给老版")
    @GetMapping(value = "/ccToBoss")
    public Object ccToBoss(@RequestParam Integer processId) throws Exception {
        holidayProcess.carbonCopy(processId, "打杂工", "老板");
        return "ok";
    }

    @ApiOperation(value = "查询历史")
    @GetMapping(value = "/history")
    public Object history(@RequestParam Integer processId) throws Exception {
        return holidayProcess.getHistory(processId);
    }

    @ApiOperation(value = "流程暂停")
    @GetMapping(value = "/pause")
    public Object pause(@RequestParam Integer processId) throws Exception {
        holidayProcess.pauseProcess(processId, "admin");
        return "ok";
    }

    @ApiOperation(value = "流程继续")
    @GetMapping(value = "/continue")
    public Object continueProc(@RequestParam Integer processId) throws Exception {
        holidayProcess.continueProcess(processId, "admin");
        return "ok";
    }

    @ApiOperation(value = "流程字典查询")
    @GetMapping(value = "/dictMap")
    public Object dictMap() throws Exception {
        return ProcessDict.getAllProcessDict();
    }

}
